package com.yanwq.sayhi.rest;

import com.yanwq.sayhi.bean.Article;
import com.yanwq.sayhi.bean.Comment;
import com.yanwq.sayhi.exception.ExResponse;
import com.yanwq.sayhi.service.CommentService;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 作者：yanweiqiang。
 * 日期：2016/2/24。
 */
@RestController
@RequestMapping(path = "/comment")
public class CommentRest {

    @Autowired
    private CommentService service;

    @RequestMapping(path = "/add")
    public Comment add(@RequestBody Comment comment) {
        if (TextUtils.isEmpty(comment.getContent())) {
            throw new ExResponse(101, "吐槽内容不能为空");
        }

        comment.setUuid(UUID.randomUUID().toString());
        comment.setPublishTime(System.currentTimeMillis());
        return service.save(comment);
    }

    @RequestMapping(path = "/list")
    public List<Comment> list(@RequestParam long articleId, @RequestParam int page, @RequestParam int size) {
        Article article = new Article();
        article.setId(articleId);
        return service.listByArticle(article, page, size);
    }
}
